<?php

class News extends CI_Controller {

    private static $layout_data = array();
    private static $home_data = array();
    
    function __construct()
    {
        parent::__construct();

        $this->load->model('node_model');
    }
	
    function index()
    {
        //Set params
        $ctegoryID = $this->input->get('categoryID')?$this->input->get('categoryID'):8;
        $limit = $this->input->get('limit')?$this->input->get('limit'):10;
        $offset = $this->uri->segment(4)?$this->uri->segment(4):0;

        //Get arrNode data and total
        $arrNode = $this->node_model->getListNodeByCategoryID($ctegoryID, $limit, $offset);
        
        //Check total and load paging
        if($arrNode['total'] > $limit)
        {
            $this->load->library('pagination');

            $config['base_url'] = site_url() . '/news/index/offset/';
            $config['total_rows'] = $arrNode['total'];
            $config['per_page'] = $limit;
            $config['uri_segment'] = 4;

            $this->pagination->initialize($config);
            self::$home_data['paging'] = TRUE;
        }

        //Set homedata
        self::$home_data['arrNode'] = $arrNode;

        //Assgin to view
        self::$layout_data['page_title'] = 'Oneway.vn - Tin Tức';
        self::$layout_data['content'] = $this->load->view('news/index', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }

    /**
     * Get new news ajax
     */
    public function getBlockHomeNews()
    {
        //Set params
        $ctegoryID = $this->input->get('categoryID')?$this->input->get('categoryID'):8;
        $limit = $this->input->get('limit')?$this->input->get('limit'):10;
        $offset = $this->input->get('offset')?$this->input->get('offset'):0;

        //Get arrNode data and total
        $arrNode = $this->node_model->getListNodeByCategoryID($ctegoryID, $limit, $offset);

        self::$home_data['arrNode'] = $arrNode['data'];

        //Assign to view
        $this->load->view('news/left_block', self::$home_data);
    }

    /**
     * View detail function
     */
    public function view()
    {
        //Get nodeID
        $nodeID = $this->uri->segment(4);

        //Get note detail
        $nodeDetail = $this->node_model->getNodeDetail($nodeID);

        //Assign to view
        self::$home_data['node'] = $nodeDetail;
        self::$layout_data['page_title'] = $nodeDetail['title'];
        self::$layout_data['page_description'] = $nodeDetail['description'];
        self::$layout_data['page_thumb'] = $nodeDetail['thumb'];
        self::$layout_data['content'] = $this->load->view('news/view', self::$home_data, TRUE);

        //Load view
        $this->load->view('layouts/main', self::$layout_data);
    }

    /**
     * Get list node older
     */
    public function getListNodeOlder()
    {
        $nodeID = $this->input->get('nodeID');
        $link = $this->input->get('link');

        //Get data
        self::$home_data['arrNode'] = $this->node_model->getListNodeOlder($nodeID);
        self::$home_data['link'] = $link;

        //Assign to view
        $this->load->view('news/list_older', self::$home_data);

    }

    /**
     * GEt list hot node
     */
    public function getListHotNode()
    {
        //Get params
        $limit = $this->input->get('limit');

        //Get data
        $arrNode = $this->node_model->getListHotNode(10);
        
        //Get random
        shuffle($arrNode);
        
        //Get limit and random
        self::$home_data['arrNode'] = array_slice($arrNode, 0, $limit);

        //Assign to view
        $this->load->view('news/list_hot', self::$home_data);
    }

}
